//
//  ESHoroscopeSearchViewController.m
//  EasyStyle
//
//  Created by FK on 16/6/4.
//  Copyright © 2016年 FK. All rights reserved.
//

#define REUSE_ID @"reuse"

#import "ESHoroscopeSearchViewController.h"
#import "RGCollectionViewCell.h"

@interface ESHoroscopeSearchViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
//@property NSMutableArray *listOfData;
@property (nonatomic, strong)UIScrollView *mainScrollView;

@property (nonatomic, strong)UILabel *horosNameLael;
@property (nonatomic, strong)UILabel *summaryLabel;


/// 为了答辩的临时数据
//@property (nonatomic, strong)NSArray *summaryList;

@end

@implementation ESHoroscopeSearchViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    
    //    UIView *viewFrame = [[UIView alloc]initWithFrame:self.collectionView.bounds];
    //    self.collectionView.backgroundView = viewFrame;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //    [self.collectionView setContentOffset:CGPointMake(width, 0.0F)];


    [self setupRootView];
    
    [self setupMainScrollViewContent:self.mainScrollView];
}

#pragma mark -- 布局相关
- (void)setupRootView
{
//    self.view.backgroundColor = [UIColor colorWithHex:@"e7e3d8"];
//
//    
    NSInteger startY = 0;
    
    /// 导航栏
    WS(weakSelf);
    self.navigationController.navigationBarHidden = YES;
    [self addCustomNavigationBarWithStyle:DefaultStyle customColor:[UIColor colorWithHex:@"3ec5dc"]];
    [self.sgCustomNavigationBar showBackButtonWithActionBlock:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    [self.sgCustomNavigationBar setTitle:@"星座运势"];
    self.sgCustomNavigationBar.titleLabel.textColor = [UIColor whiteColor];
    
    startY += self.navigationController.navigationBar.height + StatusBarHeight;
    
    /// 背景
    UIVisualEffect *effect = [UIBlurEffect effectWithStyle:1];
    UIVisualEffectView *mainEffectView = [[UIVisualEffectView alloc]initWithEffect:effect];
    mainEffectView.frame =  CGRectMake(0, startY, ScreenWidth, ScreenHeight - startY);

    UIImageView *mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, startY, ScreenWidth, ScreenHeight - startY)];
    mainImageView.image = [UIImage imageNamed:@"WeChatTableViewBG"];
    
    [self.view addSubview:mainImageView];
    [self.view addSubview:mainEffectView];

    
    
    /// mainScrollView
    _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, startY, ScreenWidth, ScreenHeight - startY - self.collectionView.height)];
    _mainScrollView.backgroundColor = [UIColor clearColor];
    _mainScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;;
    _mainScrollView.alpha = 1;
    _mainScrollView.bounces = YES;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:_mainScrollView];
    
    
    [self.view bringSubviewToFront:self.collectionView];
    
    
//    [self.view insertSubview:_mainScrollView belowSubview:self.collectionView];

}


- (void)setupMainScrollViewContent:(UIScrollView *)parentView
{
    NSInteger startX = 25;
    NSInteger startY = 0;
    NSInteger endx = ScreenWidth - startX;
    
    ///星座名称
    startY += 20;
    _horosNameLael = [[UILabel alloc]initWithFrame:CGRectMake(startX, startY, endx - startX, 50)];
    _horosNameLael.backgroundColor = [UIColor clearColor];
    _horosNameLael.textColor = [UIColor whiteColor];
    _horosNameLael.textAlignment = NSTextAlignmentCenter;
    _horosNameLael.font = ESFontWithSize(22);
    _horosNameLael.text = [indexConsNameDict objectForKey:[NSNumber numberWithInteger:0]];

    [parentView addSubview:_horosNameLael];
    startY += _horosNameLael.height;
    
    startY += 15;
    _summaryLabel = [[UILabel alloc]initWithFrame:CGRectMake(startX, startY, endx - startX, 200)];
    _summaryLabel.backgroundColor = [UIColor clearColor];
    _summaryLabel.textColor = [UIColor whiteColor];
    _summaryLabel.textAlignment = NSTextAlignmentLeft;
    _summaryLabel.font = ESFontWithSize(18);
    _summaryLabel.numberOfLines = 0;
    _summaryLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _summaryLabel.text = [NSString stringWithFormat:@"  %@",indexConsDetailData[0]];
    
    [parentView addSubview:_summaryLabel];
    
    startY += _summaryLabel.height;
    
    
}

#pragma mark -- 事件函数
- (void)changeHorosDataWithIndex:(NSInteger)index
{
    NSString *horosName = [indexConsNameDict objectForKey:[NSNumber numberWithInteger:index]];
    [UIView animateWithDuration:0.2 animations:^{
        _horosNameLael.left = _horosNameLael.left - 5;
        _summaryLabel.left = _summaryLabel.left - 5;
        _horosNameLael.alpha = 0;
        _summaryLabel.alpha = 0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.4 animations:^{
            _horosNameLael.left = _horosNameLael.left + 5;
            _summaryLabel.left = _summaryLabel.left + 5;
            _horosNameLael.alpha = 1;
            _summaryLabel.alpha = 1;
            _horosNameLael.text = horosName;
            _summaryLabel.text = [NSString stringWithFormat:@"  %@",indexConsDetailData[index]];
        } completion:^(BOOL finished) {

        }];
  
    }];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  indexConsNameDict.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:REUSE_ID forIndexPath:indexPath];
    
    [self configureCell:cell forItemAtIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(RGCollectionViewCell *)cell
   forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *nameOfImage = [indexConsNameDict objectForKey:[NSNumber numberWithInteger:indexPath.section]];
    
    cell.imageView.image = [UIImage imageNamed:nameOfImage];
    cell.label.text = [indexConsDateDict objectForKey:nameOfImage];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self changeHorosDataWithIndex:indexPath.section];
    NSLog(@"selectItem == %ld",(long)indexPath.section);
}
//- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSLog(@"2222selectItem == %ld",(long)indexPath.section);
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
